package com.example.mall.controller;

import com.example.mall.common.api.CommonResult;
import com.example.mall.dto.ChartDto;
import com.example.mall.mbg.model.Data;
import com.example.mall.mbg.model.Score;
import com.example.mall.mbg.model.Supplier;
import com.example.mall.service.ScoreService;
import com.example.mall.service.SupplierService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
@RequestMapping({"/chart"})
public class ChartController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private SupplierService supplierService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ChartController.class);

    @ApiOperation("画面：供应商排行")
    @RequestMapping(value = {"list"},method = {RequestMethod.GET})
    public String list(Model model, HttpSession session) {
        List<String> periodList = scoreService.getPeriodList();
        model.addAttribute("periodList", periodList);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "supplier-list-chart";
    }

    @ApiOperation("图标list数据")
    @RequestMapping(value = "/datalist", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult datalist(String period) {
        CommonResult commonResult;
        Map<String, Object> res = new HashMap<>();
        try {
            // 所有公司的评分
            List<ChartDto> chartList = scoreService.getChartList(period);
            List<String> suppliernameList = new ArrayList<>();
            List<Integer> indicator1List = new ArrayList<>();
            List<Integer> indicator2List = new ArrayList<>();
            List<Integer> indicator3List = new ArrayList<>();
            List<Integer> indicator4List = new ArrayList<>();
            List<Integer> indicator5List = new ArrayList<>();
            List<Integer> indicator6List = new ArrayList<>();
            List<Integer> totalscoreList = new ArrayList<>();
            chartList.forEach(chartDto -> {
                if (!suppliernameList.contains(chartDto.getSuppliername())) {
                    suppliernameList.add(chartDto.getSuppliername());
                }
                switch (chartDto.getIndicatorid()) {
                    case 1:
                        indicator1List.add(chartDto.getScore().intValue());
                        break;
                    case 2:
                        indicator2List.add(chartDto.getScore().intValue());
                        break;
                    case 3:
                        indicator3List.add(chartDto.getScore().intValue());
                        break;
                    case 4:
                        indicator4List.add(chartDto.getScore().intValue());
                        break;
                    case 5:
                        indicator5List.add(chartDto.getScore().intValue());
                        break;
                    case 6:
                        indicator6List.add(chartDto.getScore().intValue());
                        break;
                }

            });

            List<Score> scoreList = scoreService.getScoreList();
            scoreList.forEach(score -> {
                totalscoreList.add(score.getTotalscore().intValue());
            });
            res.put("suppliernameList", suppliernameList);
            res.put("indicator1List", indicator1List);
            res.put("indicator2List", indicator2List);
            res.put("indicator3List", indicator3List);
            res.put("indicator4List", indicator4List);
            res.put("indicator5List", indicator5List);
            res.put("indicator6List", indicator6List);
            res.put("totalscoreList", totalscoreList);
            commonResult = CommonResult.success(res);
            LOGGER.debug("createData success:{}", res);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createData failed:{}", res);
        }
        return commonResult;
    }

    @ApiOperation("画面：供应商细节")
    @RequestMapping(value = {"detail"},method = {RequestMethod.GET})
    public String detail(Model model, HttpSession session) {
        List<Supplier> supplierList = supplierService.listAll();
        List<String> periodList = scoreService.getPeriodList();
        model.addAttribute("periodList", periodList);
        model.addAttribute("supplierList", supplierList);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "supplier-chart";
    }

    @ApiOperation("图表detail数据")
    @RequestMapping(value = "/datadetail", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult datadetail(Integer supplierid, String period) {
        CommonResult commonResult;
        Map<String, Object> res = new HashMap<>();
        List<String> indicatornameList = new ArrayList<>();
        List<Integer> indicatorList = new ArrayList<>();
        try {
            List<ChartDto> chartDetail = scoreService.getChartDetail(supplierid,period);
            if (chartDetail.size() > 0) {
                String suppliername = chartDetail.get(0).getSuppliername();
                chartDetail.forEach(chartDto -> {
                    indicatornameList.add(chartDto.getIndicatorname());
                    indicatorList.add(chartDto.getScore().intValue());
                });
                res.put("suppliername", suppliername);
                res.put("indicatornameList", indicatornameList);
                res.put("indicatorList", indicatorList);
            } else {
                res.put("suppliername", "无数据");
                res.put("indicatornameList", indicatornameList);
                res.put("indicatorList", indicatorList);
            }
            commonResult = CommonResult.success(res);
            LOGGER.debug("createData success:{}", res);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createData failed:{}", res);
        }
        return commonResult;
    }
}
